package igor.mass.render.validators;

import igor.mass.model.AndroidParam;
import igor.mass.render.renderables.AndroidRenderable;

public class StringValidator extends AndroidValidator{
	public static int NOERROR=-1, ERR_MANDATORY=0, ERR_LENGTH=1;
	int errorType=NOERROR;
	
	
	public StringValidator(AndroidRenderable renderable, AndroidParam param){
		super(renderable, param);
	}
	
	@Override
	public boolean validate() {
		String text = renderable.getValue();
		
		if (param.mandatory == true && text.length() == 0) {
			errorType = ERR_MANDATORY;
			return false;
		}
		if (param.length >0 && param.length < text.length()) {
			errorType = ERR_LENGTH;
			return false;
		}
		errorType = NOERROR;
		return true;
	}
	
	@Override
	public String getErrorMsg() {
		if (validate())
			return null;
		if (errorType == ERR_LENGTH) return "Must be less than "+param.length+" characters";
		if (errorType == ERR_MANDATORY) return "Field cannot be left blank";
		return null;
	}

}
